﻿using blackhouse.BlackApart.WebNemo;
using blackhouse.mojoPortalExtend.Urls;
using blackhouse.WebNemo.Business.Groups;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackhouse.StareKino.Web {
    public class NemoApartmentTypeExtension : RecordExtension {

        #region Fields

        NemoApartmentType apartmentType;

        #endregion

        #region Constructors

        public NemoApartmentTypeExtension(GroupRecord record)
            : base(record) {
            this.SetApartmentType(record);
        }

        #endregion

        #region Methods

        protected override string GetIdentyficator(GroupRecord record) {
            this.SetApartmentType(record);
            return this.apartmentType.Name;
        }

        protected override void RecordChanged(GroupRecord record, RecordChangeType type) {
            this.SetApartmentType(record);

            if (type == RecordChangeType.Added || type == RecordChangeType.Updated) {
                ApartmentTypeUrlSupport url = new ApartmentTypeUrlSupport(this.apartmentType);
                if (!String.IsNullOrEmpty(this.apartmentType.Url))
                    url.SetUserNewUrl(ApartmentTypeUrlSupport.FormatUrl(this.apartmentType.Url));
                UrlSupportProvider urlProvider = new UrlSupportProvider(url);
                urlProvider.Save();
            }

            if (type == RecordChangeType.Deleted) {
                FriendlyUrl urlManage = new FriendlyUrl(CacheHelper.GetCurrentSiteSettings().SiteId, record.GetString("page_url").ReplaceEx("~/", ""));
                if (urlManage.FoundFriendlyUrl)
                    FriendlyUrl.DeleteUrl(urlManage.UrlId);
            }
        }

        private void SetApartmentType(GroupRecord record) {
            if (record != null && (this.apartmentType == null || record.RecordId != this.apartmentType.RecordId))
                this.apartmentType = new NemoApartmentType(record.GetGroup(), record);
        }

        protected override void RecordTranslated(GroupRecord record, GroupColumn column, string prevValue, string value) { }

        #endregion

    }
}
